/**   
 * @Title: ShuangseqiuHemaiLannchCommunicater.java 
 * @Package com.esun.rabbit2.communicaters 
 * @Description: (通讯接口10002) 
 * @author: City Jiangsy@500wan.com   
 * @date: 2011-6-29 下午01:23:42  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.communicaters;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import com.esun.config.BussinessCode;
import com.esun.config.Config;
import com.esun.config.DistributionChannels;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.ShuangseqiuInfo;
import com.esun.model.UserInfo;
import com.esun.network.ServerCommunicater;

/**
 * @ClassName: ShuangseqiuHemaiLannchCommunicater
 * @Description: (双色球合买发起)
 * @author: City Jiangsy@500wan.com
 * @date: 2011-6-29 下午01:23:42
 * 
 */
public class ShuangseqiuHemaiLannchCommunicater extends ServerCommunicater {
	/*
	 * expect varchar(20) 期号 必填 zhushu int(10) 注数 必填 code text 投注号码 必填 allmoney float(10) 发起方案总金额 必填 beishu int(10) 购彩倍数 (合买必填)
	 * 
	 * allnum int(10) 总份数 (合买必填) buynum int(10) 发起购买份数 (合买必填) baodinum int(10) 保底份数 (合买必填 大于0为保底) ticheng int 提成 选填() isshow Int 是否公开显示 默认为0 title varchar(20) 方案标题 选填 content
	 * varchar(20) 方案内容 选填
	 */

	private String msgServer; // 服务器信息
	private boolean isSucceed = false;

	public String getMsgServer() {
		return msgServer;
	}

	public void setMsgServer(String msgServer) {
		this.msgServer = msgServer;
	}

	public boolean isBuySucceed(int zhushu, int beishu, int allMoney, int allNum, int buyNum, int baodiNum, int ticheng, int isShow) throws ClientProtocolException, IOException,
			ParserConfigurationException, SAXException {
		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		list.add(mapItem(BussinessCode.PASER_MID, BussinessCode.MID_SHUANGSEQIU_HEMAI));
		list.add(mapItem(BussinessCode.PASER_CK, UserInfo.getUserInfo().getUserSession()));
		list.add(mapItem(Key.KEY_EXPECT, ShuangseqiuInfo.getShuangseqiuInfo().getTermNum()));
		list.add(mapItem(Key.KEY_ZHUSHU, Integer.toString(zhushu)));
		list.add(mapItem(Key.KEY_ALLMONEY, Integer.toString(allMoney)));
		list.add(mapItem(Key.KEY_ALLNUM, Integer.toString(allNum)));
		list.add(mapItem(Key.KEY_BUYNUM, Integer.toString(buyNum)));
		list.add(mapItem(Key.KEY_BAODINUM, Integer.toString(baodiNum)));
		list.add(mapItem(Key.KEY_TICHENG, Integer.toString(ticheng)));
		list.add(mapItem(Key.KEY_ISSHOW, Integer.toString(isShow)));
		list.add(mapItem(Key.KEY_BEISHU, Integer.toString(beishu)));
		list.add(mapItem(Key.KEY_TITLE, Language.STR_HEMAI_TITLE));
		list.add(mapItem(Key.KEY_CHANNEL, DistributionChannels.QUDAO));
		list.add(mapItem(BussinessCode.PASER_LIST, rowOutput(ShuangseqiuInfo.getShuangseqiuInfo().getLotteryList())));
		// 写入服务器返回信息
		super.getServerResponse(list, true);
		setMsgServer(super.getErrorMSG());
		if (super.getState() == 100) {
			isSucceed = true;
			UserInfo.getUserInfo().setUserBalance(super.getResult().get(Key.KEY_MONEY));
		} else {
			isSucceed = false;
		}

		return isSucceed;

	}

	/**
	 * @Title: mapItem
	 * @Description: (工具类,用于构建AraayList单条内容)
	 * @param @param name 参数名
	 * @param @param value 参数值
	 * @param @return map
	 * @return HashMap<String,String> 返回类型
	 * @throws
	 */
	private HashMap<String, String> mapItem(String name, String value) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(name, value);
		return map;
	}

	private String rowOutput(List<HashMap<String, Object>> list) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			String red = ((String) list.get(i).get(Key.KEY_RED)).replace(Config.ONESPACE, Config.COMMA);
			String blue = ((String) list.get(i).get(Key.KEY_BLUE)).replace(Config.ONESPACE, Config.COMMA);
			sb.append(Config.LEFT_ABR).append(BussinessCode.PASER_ROW).append(Config.ONESPACE).append(Key.KEY_CODE).append(Config.EQUAL_SIGN).append(Config.QUOTATION_MARKS).append(red)
					.append(Config.VERTRAL_LINE).append(blue).append(Config.QUOTATION_MARKS).append(Config.SLASH_LINE).append(Config.RIGHT_ABR);
		}
		String result = sb.toString();
		return result;

	}

}
